﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace VisionCloud.Utility
{
    public class JsonHelper
    {
        public static string ReadJsonFromFile(string path)
        {
            string jsonStr = string.Empty;
            string line;
            using StreamReader r1 = new StreamReader(path);
            while ((line = r1.ReadLine()) != null)
            {
                jsonStr += line;
            }
            return jsonStr;
        }

        /// <summary>
        /// 从Json文件读取到实体类，解析可能会出现异常
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path"></param>
        /// <returns></returns>
        public static T ReadObjectFromJsonFile<T>(string path)
        {
            string jsonStr = ReadJsonFromFile(path);
            return JsonConvert.DeserializeObject<T>(jsonStr);
        }
    }
}
